
***************************************************************************************************
****                                         OUTLINE                                           ****
****___________________________________________________________________________________________****
****                                                                                           ****
****      I. Program Installation                                                              ****
****      II.Data Preparation                                                                  ****
****      III. Generate Related Variables                                                      ****
****                                                                                           ****
***************************************************************************************************

clear all
set more off
cap log close

cd "C:\Users\wie-dainn\Dropbox\Work\Pleng\Article Version"

*******************************

/*
***************************************************************************************************
****      I. Program Installation                                                              ****
***************************************************************************************************
cap ado uninstall ftools
net install ftools, from("https://raw.githubusercontent.com/sergiocorreia/ftools/master/src/")
cap ado uninstall reghdfe
net install reghdfe, from("https://raw.githubusercontent.com/sergiocorreia/reghdfe/master/src/")
cap ado uninstall ivreg2
ssc install ivreg2
cap ado uninstall ivreghdfe
net install ivreghdfe, from(https://raw.githubusercontent.com/sergiocorreia/ivreghdfe/master/src/)
cap ado uninstall ranktest
ssc install ranktest
ssc install outreg2
ssc install asdoc, replace
ssc install winsor2
set more off
*/

*******************************

***************************************************************************************************
****      II.Data Preparation                                                                  ****
***************************************************************************************************
use "Data\merged.dta", clear

keep year month province region MW pMW yos age sex member employ main_hr other_hr total_hr ot bonus other_money FOOD CLOTH HOUSE OTH_THIN GRADE_A GRADE_B status cpi area member wage_day wage_month lnwage_month regworker indus_label edu_label marital PSU_NO weight industry district sub_district ED BLK_VIL HH_NO TYPE LISTING RELATION

label var member "Family size"
label var age "Age"
label var sex "Men"
label var main_hr "Working hour of main job"
label var other_hr "Working hour of other job"
label var total_hr "Total working hour"
label var yos "Years of schooling"
gen female = 1
replace female = 0 if sex == 1
replace sex = 0 if sex == 2

*Minimum Wage
gen MW_month = MW*20
label var MW_month "Minimum wage"
gen rMW_month = MW_month*100/cpi
label var rMW_month "Real minimum wage"
gen lnMW_month = ln(MW_month)
label var lnMW_month "Log minimum wage" 
gen lnrMW_month = ln(rMW_month)
label var lnrMW_month "Log real minimum wage"

*Wage
gen rwage_month = wage_month*100/cpi
label var rwage_month "Real wage of main job"
gen lnrwage_month = ln(rwage_month)
label var lnrwage_month "Log real wage of main job"
gen total_wage = wage_month+bonus+ot+other_money
label var total_wage "Total wage per month"
gen rtotal_wage = total_wage*100/cpi
label var rtotal_wage "Total wage per month in real term"
gen lntotal_wage = ln(total_wage*100/cpi)
label var lntotal_wage "Log of real total wage"

*Employment
gen employ2=0
replace employ2=1 if status>=4 & employ == 1
gen employ3=0
replace employ3=1 if status>1 & status<=3 & employ == 1
label var employ "Employed"
label var employ2 "Employed as a paid worker"
label var employ3 "Unpaid worker or self-employed"
label var status "Status of employment"

*School Dropout
gen schdrop = 0
replace schdrop = 1 if GRADE_A == 001
label var schdrop "School Dropout (Not Going to School == 1)"

*1 Digit Industry id
tostring industry, gen(indus)
gen indusid = real(substr(indus,1,1))
drop indus

winsor2 wage_month, replace cuts(1 99) trim
winsor2 main_hr, replace cuts(1 99) trim
winsor2 total_hr, replace cuts(1 99) trim
winsor2 total_wage, replace cuts(1 99) trim

keep if year>=2010 & year<=2016
drop if province==43 | province==38

gen paid = 0
replace paid = 1 if lntotal_wage!=.

*Create Household id
tostring province, generate(provinceid)
tostring district, generate(districtid)
tostring sub_district, generate(sub_districtid)
tostring area, generate(areaid)
tostring ED, generate(EDid)
tostring BLK_VIL, generate(BLK_VILid)
tostring year, generate(yearid)
tostring month, generate(monthid)
tostring HH_NO, generate(HH_NOid)
egen hhid = concat(provinceid districtid sub_districtid areaid EDid yearid monthid BLK_VILid HH_NOid)
label var hhid "Household id"
sort hhid
drop provinceid districtid sub_districtid areaid EDid yearid monthid BLK_VILid HH_NOid

*Household head education
gen edu_temp=edu_label if RELATION==1
by hhid, sort: egen hh_edu=max(edu_temp)
gen HSESr=(hh_edu>=3)
gen HSESu=(hh_edu>=4)


***************************************************************************************************
****      III. Generate Related Variables                                                      ****
***************************************************************************************************

gen workage = 0 if year == 2011
replace workage = 1 if age >= 15 & age <= 64 & year == 2011
bysort province: egen workage_byp = sum(workage)

gen lf = 0 if year == 2011
replace lf = 1 if employ == 1 & year == 2011
bysort province: egen LFp = sum(lf)

gen lp = 0 if year == 2011
replace lp = 1 if workage == 1 & employ == 1 & year == 2011
bysort province: egen lp_byp = sum(lp)
bysort province: gen EP_share = lp_byp/workage_byp
bysort province: egen EP = max(EP_share)
drop workage_byp lp_byp EP_share lf workage

tab year, gen(yr)
gen EP13=EP*yr3
gen EP14=EP*yr4
gen EP15=EP*yr5
gen EP16=EP*yr6

*Share of Effected Workers by Province in 2011 (effectp)   
gen effect = 0 if year == 2011
replace effect = 1 if wage_day < 300 & year == 2011
replace effect = 1 if wage_month < 6000 & year == 2011
bysort province: egen effect_byp = sum(effect)
bysort province: gen effect_share = effect_byp/LFp
bysort province: egen effectp = max(effect_share)
drop effect effect_byp effect_share 

gen effectp13=effectp*yr3
gen effectp14=effectp*yr4
gen effectp15=effectp*yr5
gen effectp16=effectp*yr6

*Share of people in Manufacturing Sector by Province in 2011 (manup)
gen manu = 0 if year == 2011
replace manu = 1 if indus_label == 2 & year == 2011
bysort province: egen manu_byp = sum(manu)
bysort province: gen manu_share = manu_byp/LFp
bysort province: egen manup = max(manu_share)
drop manu manu_byp manu_share

gen manup13=manup*yr3
gen manup14=manup*yr4
gen manup15=manup*yr5
gen manup16=manup*yr6

*EMW Change
gen w=.
replace w=((300-pMW)/cpi)  if year==2011
by province, sort: egen pMWc=max(w)
replace pMWc=0 if year==2011
gen taup=effectp*pMWc

gen taup13=taup*yr3
gen taup14=taup*yr4
gen taup15=taup*yr5
gen taup16=taup*yr6

label var taup13 "EMW Change * Year2013"
label var taup14 "EMW Change * Year2014"
label var taup15 "EMW Change * Year2015"
label var taup16 "EMW Change * Year2016"

drop w LFp

*Interaction Terms
gen tauph=taup*HSESr
gen tauph13=taup13*HSESr
gen tauph14=taup14*HSESr
gen tauph15=taup15*HSESr
gen tauph16=taup16*HSESr

label var tauph13 "EMWp * Year2013 * HSES (Rural)"
label var tauph14 "EMWp * Year2014 * HSES (Rural)"
label var tauph15 "EMWp * Year2015 * HSES (Rural)"
label var tauph16 "EMWp * Year2016 * HSES (Rural)"

gen taupuh=taup*HSESu
gen taupuh13=taup13*HSESu
gen taupuh14=taup14*HSESu
gen taupuh15=taup15*HSESu
gen taupuh16=taup16*HSESu

label var taupuh13 "EMWp * Year2013 * HSES (Urban)"
label var taupuh14 "EMWp * Year2014 * HSES (Urban)"
label var taupuh15 "EMWp * Year2015 * HSES (Urban)"
label var taupuh16 "EMWp * Year2016 * HSES (Urban)"

save "Data/data.dta", replace



